package com.sos.core.cards
{
	
	import com.sos.core.Role;
	import com.sos.core.School;
	import com.sos.core.Stats;

	public class PlayerCard extends ExpCard
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// Default values
		protected static const		DEFAULT_ROLE:Role					= Role.NONE;
		protected static const		DEFAULT_ACTION_TEXT:String			= "";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		protected var _role:Role;
		protected var _stats:Stats;
		protected var _actionText:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function PlayerCard(kind:CardKind = null)
		{
			// Call ancestor
			super(kind);
			// Init
			_type = CardType.PLAYER;
			_stats = new Stats();
			// 
			_schools.push(School.GENERIC);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get actionText():String
		{
			return _actionText;
		}
		
		public function set actionText(value:String):void
		{
			_actionText = value;
		}
		
		public function get role():Role
		{
			return _role;
		}
		
		public function set role(value:Role):void
		{
			_role = value;
		}
		
		public function get stats():Stats
		{
			return _stats;
		}
		
		public function set stats(value:Stats):void
		{
			_stats = value;
		}
		
		public function get stamina():int
		{
			return _stats.stamina;
		}
		
		public function get dribiling():int
		{
			return _stats.dribiling;
		}
		
		public function get passing():int
		{
			return _stats.passing;
		}
		
		public function get scoring():int
		{
			return _stats.scoring;
		}
		
		public function get defence():int
		{
			return _stats.defence;
		}
		
		public function get goalkeeping():int
		{
			return _stats.goalkeeping;
		}
		
		override public function get singleSchool():School
		{
			return _schools[1];
		}
		
		override public function get isSingleSchool():Boolean
		{
			return _schools.length == 2; // School.GENERIC is always present in PlayerCard 
		}
		
		override public function get isMultiSchool():Boolean
		{
			return	_schools.length > 2;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		// DATA OBJECT IMPLEMENTATION

		override protected function validate(data:Object):void
		{
			// Call ancestor
			super.validate(data);
			// Validating data
			if (!data.hasOwnProperty(JSON_PROPERTY_ROLE)) 		throwLoadMissingPropError("role");
			if (!data.hasOwnProperty(JSON_PROPERTY_SCHOOL)) 	throwLoadMissingPropError("school");
			if (!data.hasOwnProperty(JSON_PROPERTY_ACTIONTEXT))	throwLoadMissingPropError("action text");
		}
			
		override protected function fill(data:Object):void
		{
			// Call ancestor
			super.fill(data);
			// Load data
			_role = Role.getBySign(data[JSON_PROPERTY_ROLE]);
			// _school = School.getById(data[JSON_PROPERTY_SCHOOL]);
			_actionText = data[JSON_PROPERTY_ACTIONTEXT];
			// LT_OLD_SCHOOL : _school 	= School.getById(data[JSON_PROPERTY_SCHOOL]);
			// Check if the data contains a single school or a list of schools
			if (data[JSON_PROPERTY_SCHOOL] is Array)
			{
				// Retrieve the school list
				var rawSchools:Array = data[JSON_PROPERTY_SCHOOL] as Array;
				// Adds all the schools to the card
				for (var i:int=0; i<rawSchools.length; i++)
				{
					var school:School = School.getById(rawSchools[i]);
					addSchool(school);
				}
			}
			else
			{
				// Adds a single school to the card (old getCollection.php implementation)
				addSchool(School.getById(data[JSON_PROPERTY_SCHOOL]));
			}
			// Loads stats
			_stats = new Stats();
			_stats.load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		override public function copy(card:Card):void
		{
			super.copy(card);
			// Cast the card
			var player:PlayerCard = card as PlayerCard;
			// Copy properties
			_role			= player.role;
			_actionText		= player.actionText;
			_stats			= player.stats.clone();
		}
		
		override public function clone():Card
		{
			var clone:PlayerCard = new PlayerCard();
			clone.copy(this);
			return clone;
		}
		
		override public function clear():void
		{
			// Ancestor
			super.clear();
			// Clears data
			_role			= DEFAULT_ROLE;
			_actionText		= DEFAULT_ACTION_TEXT;
			// Clears objects
			_stats.clear();
		}
		
	}
}